const db = require("../utils/db");

// 科室-获取科室列表
exports.departmentListGet = async (params) => {
    return new Promise(((resolve, reject) => {
        let sql = '';
        if (params.departName && params.departCode) {
            sql = `select * from department where d_id like '%${params.departCode}%' and d_name like '%${params.departName}%' and d_hospital = ${params.d_hospital}`
        } else if (params.departName) {
            sql = `select * from department where d_name like '%${params.departName}%' and d_hospital = ${params.d_hospital}`
        } else if (params.departCode) {
            sql = `select * from department where d_id like '%${params.departCode}%' and d_hospital = ${params.d_hospital}`
        } else {
            sql = `select * from department where d_hospital = ${params.d_hospital}`
        }
        db.query(sql).then(res => {
            console.log(res, "departmentListGet sql查询成功结果")
            let result = {};
            result.list = res.slice((params.pageIndex - 1) * (params.pageSize), (params.pageIndex - 1) * (params.pageSize) + params.pageSize);
            result.total = res.length;
            result.pageIndex = params.pageIndex;
            result.pageSize = params.pageSize;
            resolve(result);
        }).catch(error => {
            console.log(error, "departmentListGet sql查询失败结果")
            reject(error)

        })
    }))
}
// 科室-科室添加
exports.departmentAdd = async (params) => {
    return new Promise(((resolve, reject) => {
        var value = [], key = [], sql = '';
        for (var item in params) {
            key.push(item);
            value.push(`'${params[item]}'`);
        }
        console.log(key.join(','), value.join(','));
        sql = `insert into department (${key.join(',')}) values(${value.join(',')}) `;
        db.query(sql).then(res => {
            console.log(res, "departmentAdd sql添加成功结果")
            let result = { msg: '添加成功!' };
            resolve(result);
        }).catch(error => {
            console.log(error, "departmentAdd sql添加失败结果")
            reject(error)

        })
    }))
}
// 科室-科室信息修改
exports.departmentUpdate = async (params) => {
    return new Promise(((resolve, reject) => {
        var str = [], sql = '';
        for (var item in params) {
            str.push(`${item}='${params[item]}'`);
        }
        console.log(str.join(','));
        sql += `update department set ` + str.join(',') + ` where d_id = '${params.d_id}' and d_hospital = '${params.d_hospital}'`;
        db.query(sql).then(res => {
            console.log(res, "departmentUpdate sql修改成功结果")
            let result = {};
            result.msg = '修改成功!'
            resolve(result);
        }).catch(error => {
            console.log(error, "nameIsExist sql查询失败结果");
            reject(error)

        })
    }))
}
// 科室-删除科室
exports.departmentDelete = async (params) => {
    return new Promise(((resolve, reject) => {
        let sql = `delete from department where d_id = '${params.d_id}' and d_hospital = '${params.d_hospital}'`;
        db.query(sql).then(res => {
            console.log(res, "departmentDelete sql删除成功结果")
            let result = {msg:'删除成功!'};
            resolve(result);
        }).catch(error => {
            console.log(error, "departmentDelete sql删除失败结果")
            reject(error)

        })
    }))
}